package cn.myzqu.sign.user.server.service.impl;


import cn.myzqu.sign.user.server.dao.OsAccountMapper;
import cn.myzqu.sign.user.server.dao.OsUserMapper;
import cn.myzqu.sign.user.server.entity.OsAccount;
import cn.myzqu.sign.user.server.entity.OsUser;
import cn.myzqu.sign.user.server.service.UserService;
import cn.myzqu.sign.user.server.utils.KeyUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author: 的川
 * @Date: 2019/1/6.
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private OsUserMapper osUserMapper;

    @Autowired
    private OsAccountMapper accountMapper;


    @Override
    public OsUser selectById(Integer id){
        return osUserMapper.selectById(id);
    }

    @Override
    public OsUser selectByOpenId(String openId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("open_id",openId);
        return osUserMapper.selectOne(queryWrapper);
    }

    @Override
    public Boolean register(OsUser osUser) {
        //用户昵称
        if(osUser.getNickName()==null){
            osUser.setNickName("微信用户"+ KeyUtil.getRandomCode(6));
        }
        //创建时间
        osUser.setRegisterTime(new Date());
        osUser.setUpdateTime(new Date());
        if(osUserMapper.insert(osUser)>0){
            OsAccount osAccount = new OsAccount();
            osAccount.setId(osUser.getId()+"");
            BeanUtils.copyProperties(osUser,osAccount);
            osAccount.setStatus((byte)0);
            osAccount.setOpenId(osUser.getOpenId());
            if(accountMapper.insert(osAccount)>0){
                return true;
            }
        }
        return false;
    }

    @Override
    public int updateById(OsUser record)
    {
        return osUserMapper.updateById(record);
    }


}
